/**
 * 功能:TK_GOODS 控制器类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:42:37
 */
package com.towker.frame.controller.basicData;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.controller.BaseController;
import com.towker.frame.model.basicData.Goods;
import com.towker.frame.service.basicData.GoodsBigClassService;
import com.towker.frame.service.basicData.GoodsClassService;
import com.towker.frame.service.basicData.GoodsService;

//TK_GOODS
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/basicData")
public class GoodsController extends BaseController
{
	private GoodsService service;
	private GoodsClassService goodsClassService;
	private GoodsBigClassService goodsBigClassService;
	//业务对象注入
	public void setGoodsService(GoodsService service)
	{
		this.service = service;
	}
	
	public void setGoodsClassService(GoodsClassService goodsClassService)
	{
		this.goodsClassService = goodsClassService;
	}
	public void setGoodsBigClassService(GoodsBigClassService goodsBigClassService)
	{
		this.goodsBigClassService = goodsBigClassService;
	}

	//添加
	@RequestMapping("/addGoods{viewName}")
	public ModelAndView addGoods(HttpServletResponse response, Goods po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			Map map = new HashMap();
			List goodsClassList = goodsClassService.getList(map);
			List goodsBigClassList = goodsBigClassService.getList(map);
			result.addObject("goodsClassList", goodsClassList);
			result.addObject("goodsBigClassList", goodsBigClassList);
			result.setViewName("/manage/basicData/addGoods.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.add(po);
				writer.write("添加物料成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加物料失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delGoods")
	public void delGoods(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}
	
	
	//批量修改单价
	@RequestMapping("/batchUpdJprice{viewName}")
	public ModelAndView batchUpdJprice(HttpServletRequest request, HttpServletResponse response, Goods goods, @PathVariable String viewName) throws Exception
	{
		ModelAndView mav = null;
		MyRequest req = new MyRequest(request);
		if ("1".equals(viewName)) {
			mav = new ModelAndView();
			Map map = new HashMap();
			List goodsClassList = goodsClassService.getList(map);
			mav.addObject("goodsClassList", goodsClassList);
			mav.setViewName("/manage/basicData/batchUpdJprice.jsp");
		} else if ("2".equals(viewName)) {
			String goodsSn1 = req.getString("goods_sn1");
			String goodsSn2 = req.getString("goods_sn2");
			double jprice = req.getDouble("jprice");
			double tprice_value = req.getDouble("tprice_ratio") * jprice / 100;
			double bprice_value = req.getDouble("bprice_ratio") * jprice / 100;
			double wprice_value = req.getDouble("wprice_ratio") * jprice / 100;
			double sprice_value = req.getDouble("sprice_ratio") * jprice / 100;
			
			PrintWriter writer = response.getWriter();
			try
			{
				int row = service.batchUpdJprice(goods.getType_id(), goods.getClass_id(), goodsSn1, goodsSn2, jprice, tprice_value, bprice_value, wprice_value, sprice_value);
				writer.write("批量修改物料单价成功,共修改"+row+"条！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("批量修改物料单价失败！" + e.getCause());
			}
		}

		return mav;
	}
	
	@RequestMapping("/getGoodsBybatch")
	public void getGoodsBybatch(HttpServletRequest request, HttpServletResponse response, Goods po) throws IOException {
		String goodsSn1 = request.getParameter("goods_sn1");
		String goodsSn2 = request.getParameter("goods_sn2");
		List<Goods> goods = service.getGoodsBybatch(po.getType_id(), po.getClass_id(), goodsSn1, goodsSn2);
		ObjectMapper mapper = new ObjectMapper(); 
		mapper.writeValue(response.getOutputStream(), goods);
	}
	

	//修改
	@RequestMapping("/updGoods{viewName}")
	public ModelAndView updGoods(HttpServletRequest request, HttpServletResponse response, Goods po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			Map map = new HashMap();
			List goodsClassList = goodsClassService.getList(map);
			List goodsBigClassList = goodsBigClassService.getList(map);
			result.addObject("goodsClassList", goodsClassList);
			result.addObject("goodsBigClassList", goodsBigClassList);
			result.setViewName("/manage/basicData/updGoods.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.update(po);
				writer.write("修改物料成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改物料失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getGoods")
	public ModelAndView getGoods(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<Goods> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/basicData/getGoods.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getGoodsById")
	public ModelAndView getGoodsById(HttpServletRequest request, HttpServletResponse response)
	{
		Goods po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.setViewName("/manage/basicData/getGoodsById.jsp");
		return result;
	}
	@RequestMapping("/selectGoods")
	public ModelAndView selectGoods(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page){
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<Goods> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/basicData/selectGoods.jsp");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/checkExistsGoodsSn")
	public String checkExistsCode(HttpServletRequest request, HttpServletResponse response, Goods goods){
		boolean result = service.checkExistsCode(goods.getGoods_sn(), goods.getId());
		if (result) {
			return "{\"result\":1}";
		} else {
			return "{\"result\":0}";
		}
	}
}
